package demo10_3;

import java.util.Objects;

class Person {
    public String name;
    public int age;
    public Person(String name,int age) {
        this.age = age;
        this.name = name;
    }
    //toString方法
//    @Override
//    public String toString() {
//        return "Person{" +
//                "name='" + name + '\'' +
//                ", age=" + age +
//                '}';
////        return "ok";
//    }

//    @Override
//    public boolean equals(Object obj) {
//        //如果传入的对象为空  返回false
//        if (obj == null) {
//            return false;
//        }
//        //如果两个引用指向同一个对象  返回true
//        if (this == obj) {
//            return true;
//        }
//        //如果传入对象不是Person类实例  返回false
//        if (!(obj instanceof Person)) {
//            return false;
//        }
//        //如果是Person类实例
//        //将传入对象强转成Person类
//        Person person = (Person) obj;
//        //如果名字和年龄都相同  返回true 因为name是String型所以用equals比较
//        return this.name.equals(person.name) && this.age== person.age;
//    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
class Student {}
//class Women extends Person{}

public class Test10_3 {
    public static void main(String[] args) {
        Person person1 = new Person("张三",10);
        System.out.println(person1.hashCode());
        Person person2 = new Person("张三",10);
        System.out.println(person2.hashCode());
    }
    public static void main1(String[] args) {
        Person person1 = new Person("张三",10);
        System.out.println(person1);
        Person person2 = new Person("张三",10);
        System.out.println(person2);
        System.out.println("============================");
        System.out.println(person1 == person2);
        System.out.println(person1.equals(person2));
    }
//    public static void func1(Object object) {
//        System.out.println("object");
//    }
//    public static void main1(String[] args) {
//        func1(new Student());
//        func1(new Women());
//        func1(new Person());
//    }
}
